<?php

header("Content-Type:text/html;chatset=uft-8");

class JsonRPC
{
    private $conn;

    function __construct($host, $port)
    {
        $this->conn = fsockopen($host, $port, $errno, $errstr, 3);
        if (!$this->conn) {
            return false;
        }
    }

    public function Call($method, $params)
    {
        if (!$this->conn) {
            return false;
        }
        $err = fwrite($this->conn, json_encode(
            array(
                'method' => $method,
                'params' => array($params),
                'id' => 0,
            )
        ) . "\n");
        if ($err === false) {
            return false;
        }
        stream_set_timeout($this->conn, 0, 3000);
        $line = fgets($this->conn);
        if ($line === false) {
            return NULL;
        }
        return json_decode($line, true);
    }
}

// 数据是 {"method":"hello_service.SayHello","params":["zhangsan"],"id":0} ,

$client = new JsonRPC("127.0.0.1", 8080);
$args = "this is php";
$r = $client->Call("hello_service.SayHello", $args);
print_r($r);
?>

